/*
 * TrackModel.java
 * This file is the track model for the CTCSS
 * Author: Daniel R Hartman
 * Date Created: 2012-11-24
 * Date Last Updated: 2012-12-13
 */
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;


/**
 * This class is the representation of the track for the CTCSS
 * @author Dan
 */
public class TrackModel
{

        static TrackModelPane pane;
        static Block[] red;
        static Block[] green;
        static Block[] redControllers;
        static Block[] greenControllers;

        /**
         * This is the constructor for the trackModel
         */
        public TrackModel()
        {


                pane = new TrackModelPane();

        }

        /**
         * Builds the track from the files red.csv and green.csv
         * @throws IOException
         */
        public static void build() throws IOException
        {
                int number, speedLimit, type, next, prev;
                double length, grade;
                boolean underground = false;
                String st;
                String row;
                String parsedRow[] = new String[8];
                red = new Block[77];
                green = new Block[151];
                redControllers = new Block[8];
                greenControllers = new Block[6];


                BufferedReader file = new BufferedReader(new FileReader("red.csv"));
                row = file.readLine();
                
                red[0] = new Block(1, 0, 1, 0, 55, false, 0, 0, 0);
                int i = 1;
                while(row != null)
                {
                        parsedRow = row.split(",", 9);
                        //number = Integer.parseInt(parsedRow[0]);
                        length = Double.parseDouble(parsedRow[1]);
                        grade = Double.parseDouble(parsedRow[2]);
                        speedLimit = Integer.parseInt(parsedRow[3]);
                        next = Integer.parseInt(parsedRow[7]);
                        if(parsedRow[8] == null)
                        {
                                prev = 0;
                        }else
                        {
                                prev = Integer.parseInt(parsedRow[8]);
                        }

                        if(parsedRow[4].equals("UNDERGROUND"))
                        {
                                underground = true;
                        }else
                        {
                                underground = false;
                        }
                        if(parsedRow[5].equals("station"))
                        {
                                type = 1;
                        }else if(parsedRow[5].equals("switch"))
                        {
                                type = 2;
                        }else if(parsedRow[5].equals("railcrossing"))
                        {
                                type = 3;
                        }else
                        {
                                type = 0;
                        }


                        if(!parsedRow[6].equals(""))
                        {
                                st = parsedRow[6];
                                red[i] = new Block(1, i, length, grade, speedLimit, underground, type, next, prev, st);
                        }else
                        {
                                red[i] = new Block(1, i, length, grade, speedLimit, underground, type, next, prev);
                        }
                        row = file.readLine();
                        i++;
                }
                
                for(i = 1; i< 77; i++)
                {
                        red[i].next = red[red[i].nextnum];
                        red[i].prev = red[red[i].prevnum];
                        if(red[i].type == 2)
                        {
                                red[i].switchTo = red[red[i].switchtonum];
                        }
                }

                file.close();

                file = new BufferedReader(new FileReader("green.csv"));
                row = file.readLine();

                green[0] = new Block(2, 0, 1, 0, 55, false, 0, 0, 0);
                i = 1;
                while(row != null)
                {
                        parsedRow = row.split(",", 9);
                        //number = Integer.parseInt(parsedRow[0]);
                        length = Double.parseDouble(parsedRow[1]);
                        grade = Double.parseDouble(parsedRow[2]);
                        speedLimit = Integer.parseInt(parsedRow[3]);
                        next = Integer.parseInt(parsedRow[7]);
                        if(parsedRow[8].equals(""))
                        {
                                prev = 0;
                        }else
                        {
                                prev = Integer.parseInt(parsedRow[8]);
                        }

                        if(parsedRow[4].equals("UNDERGROUND"))
                        {
                                underground = true;
                        }else
                        {
                                underground = false;
                        }
                        if(parsedRow[5].equals("station"))
                        {
                                type = 1;
                        }else if(parsedRow[5].equals("switch"))
                        {
                                type = 2;
                        }else if(parsedRow[5].equals("railcrossing"))
                        {
                                type = 3;
                        }else
                        {
                                type = 0;
                        }


                        if(!parsedRow[6].equals(""))
                        {
                                st = parsedRow[6];
                                green[i] = new Block(2, i, length, grade, speedLimit, underground, type, next, prev, st);
                        }else
                        {
                                green[i] = new Block(2, i, length, grade, speedLimit, underground, type, next, prev);
                        }
                        row = file.readLine();
                        i++;
                }

                file.close();
                
                int numcontrollers = 0;
                for(i = 1; i <= 76; i++)
                {
                        red[i].next = red[red[i].nextnum];
                        red[i].prev = red[red[i].prevnum];
                        if(red[i].type == 2)
                        {
                                red[i].switchTo = red[red[i].switchtonum];
                        }
                        if(red[i].type == 2 || red[i].type == 3)
                        {
                                redControllers[numcontrollers] = red[i];
                                numcontrollers++;
                        }
                }
                
                numcontrollers = 0;
                for(i = 1; i <= 76; i++)
                {
                        green[i].next = green[green[i].nextnum];
                        green[i].prev = green[green[i].prevnum];
                        if(green[i].type == 2)
                        {
                                green[i].switchTo = green[green[i].switchtonum];
                        }
                        if(green[i].type == 2 || green[i].type == 3)
                        {
                                greenControllers[numcontrollers] = green[i];
                                numcontrollers++;
                        }
                }

                pane = new TrackModelPane();
        }


        /**
         * This method is called by other subsystems when passing a bundle to the track model
         * @param b The bundle being passed to the trackModel
         * @throws InterruptedException
         */
        public static void receive(TMBundle b) throws InterruptedException{
        	
        }

        /**
         * This method is called by other subsystems when passing a bundle to the track model
         * @param b The bundle being passed to the trackModel
         * @throws InterruptedException
         */
        public static void receive(TKBundle b) throws InterruptedException{
                if(b.line == 1)
                {
                	if(red[b.block].closed == true)
                	{
                		red[b.block].closed = false;
                	}else
                	{
                		red[b.block].closed = true;
                	}
                }else if(b.line == 2)
                {
                	if(green[b.block].closed == true)
                	{
                		green[b.block].closed = false;
                	}else
                	{
                		green[b.block].closed = true;
                	}
                }
                TMBundle bundle = new TMBundle(b.speed, b.authority);
        }
}
